package com.fsk.starter.service.impl;

import com.alibaba.fastjson.JSON;
import com.fsk.starter.common.DateUtil;
import com.fsk.starter.common.Prefix;
import com.fsk.starter.common.RedisException;
import com.fsk.starter.service.RedisService;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.annotation.Resource;
import java.util.Date;

/**
 * redis 接口具体业务实现类
 *
 */
public class RedisServiceImpl implements RedisService {

    @Resource
    private JedisPool jedisPool;

    @Override
    public <T> void set(Prefix prefix, String key, T data) {
        try (Jedis jedis = jedisPool.getResource()) {
            // 拼接 key
            String realKey = prefix.keyPrefix() + key;
            // 转换 value
            String value = JSON.toJSONString(data);
            if (prefix.expireTime() > 0) {
                jedis.setex(realKey, prefix.expireTime(), value);
            } else {
                jedis.set(realKey, value);
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RedisException("Redis set method Exception");
        }
    }

    @Override
    public <T> T get(Prefix prefix, String key, Class<T> clz) {
        try (Jedis jedis = jedisPool.getResource()) {
            return JSON.parseObject(jedis.get(prefix.keyPrefix() + key), clz);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RedisException("Redis get method Exception");
        }
    }

    @Override
    public boolean exists(Prefix prefix, String key) {
        try (Jedis jedis = jedisPool.getResource()) {
            return jedis.exists(prefix.keyPrefix() + key);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RedisException("Redis exists method Exception");
        }
    }

    @Override
    public int signIn(Prefix prefix, String key) {
        try (Jedis jedis = jedisPool.getResource()) {
            // 拼接当天签到标识的 key
            String todayKey = prefix.keyPrefix() + key + ":today";
            // 拼接记录连续签到的 key
            String totalSignIn = prefix.keyPrefix() + key + ":totalDay";
            if (jedis.exists(todayKey)) {
                throw new RedisException("已签到");
            } else {
                // 标记当天已签到
                jedis.setex(todayKey, DateUtil.getTodaySeconds(new Date()), "1");
                if (!jedis.exists(totalSignIn)) {
                    // 记录连续签到天数
                    jedis.setex(totalSignIn, DateUtil.getTodaySeconds(new Date()) + (60 * 60 * 24), "0");
                }
                // 签到天数加 1
                int second = jedis.incr(totalSignIn).intValue();
                // 重置连续签到键值对的存活时间
                jedis.expire(totalSignIn, DateUtil.getTodaySeconds(new Date()) + (60 * 60 * 24));
                return second;
            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new RedisException("Redis signIn method Exception");
        }
    }

    @Override
    public void delete(Prefix prefix, String key) {
        try (Jedis jedis = jedisPool.getResource()) {
            jedis.del(prefix.keyPrefix() + key);
        }catch (Exception e) {
            e.printStackTrace();
            throw new RedisException("Redis delete method Exception");
        }
    }

}

